package com.xiaochong.tir.api.interceptor;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.netflix.discovery.converters.Auto;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.api.controller.UserController;
import com.xiaochong.tir.api.service.KLineTradesService;
import com.xiaochong.tir.api.service.TokenIndexRankService;
import com.xiaochong.tir.api.userclient.UserServiceClient;
import com.xiaochong.tir.api.util.SpringContextUtil;
import com.xiaochong.tir.common.data.vo.ApiResultVo;
import com.xiaochong.tir.common.data.vo.CurrencyQuotationResultVo;
import com.xiaochong.tir.common.util.ApiResultCodeUtils;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * @Description: 自选验证token是否过期
 * @Author: org
 * @CreateDate: 2018/7/27 17:58
 */
@Component
public class SelfSelectInterceptor implements HandlerInterceptor{
    @Autowired
    UserServiceClient userServiceClient;



    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        String userToken = request.getParameter("userToken");
        String flagStr ="";
        if(StringUtils.isNotBlank(userToken)){
            flagStr = userServiceClient.checkUserToken(userToken);
            //{"code":"100008","msg":"token过期重新登录","data":{"result":""},"traceId":"walian_user_208968065533497344"}
            //{"code":"000000","msg":"请求成功","data":{"result":"{\"countryNum\":\"+86\",\"createTime\":1533023594000,\"email\":\"ouyang_riguang@163.com\",\"platform\":6,\"status\":1,\"token\":\"f7e76cd6-8916-40f4-95ed-4b16bc8b7cd8\",\"updateTime\":1533023594000,\"vcode\":\"6KWATGS\"}"},"traceId":"walian_user_208977049619419136"}
            if(StringUtils.isNotBlank(flagStr)){
                JSONObject jsonObject = JSON.parseObject(flagStr);
                String code = (String) jsonObject.get("code");
                if("000000".equals(code)){//验证登录成功
                    return  true;
                }
            }
        }
        ApiResultVo resultVo = new ApiResultVo<>();
        resultVo.setDataStamp(System.currentTimeMillis());
        resultVo.setTradeId(LogTrace.getTrace().getTraceId());
        resultVo.setCode(ApiResultCodeUtils.TOKEN_ERROR.getCode());
        resultVo.setMessage(ApiResultCodeUtils.TOKEN_ERROR.getMsg());
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html; charset=utf-8");
        PrintWriter printWriter = response.getWriter();
        printWriter.write(JSON.toJSONString(resultVo));
        printWriter.flush();
        printWriter.close();
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
